var listPage = avalon.define({
	$id: "page",
	uid: "",
	tabHeight:0,
	tab:[{
		id:'tab1',
		title:'',
		show:true
	},{
		id:'tab2',
		title:'',
		show:true
	},{
		id:'tab3',
		title:'',
		show:true
	},{
		id:'tab4',
		title:'',
		show:true
	}],
	tab1:{
		receiptedPageNo: 1,
		receipted: [],
		noReceiptedData: "",
		receiptedTotal: 0,
		search:{
			status:0,
			useStatus:'1'
		}
	},
	tab2:{
		receiptedPageNo: 1,
		receipted: [],
		noReceiptedData: "",
		receiptedTotal: 0,
		search:{
			status:0,
			useStatus:'0'
		}
	},
	tab3:{
		receiptedPageNo: 1,
		receipted: [],
		noReceiptedData: "",
		receiptedTotal: 0,
		search:{
			status:1,
			useStatus:'2'
		}
	},
	tab4:{
		receiptedPageNo: 1,
		receipted: [],
		noReceiptedData: "",
		receiptedTotal: 0,
		search:{
			status:2
		}
	},
	getTabItem:function(item){
		// console.log(item, item.id, this, this[item.id], this[item.id].receipted);
		return this[item.id].receipted;
	}
});

mui.ready(function() {
	var uid = $("#uid").val();
	listPage.uid = uid;
	var _a = $.getUrlParam("a");
	if(!_a) {
		$('#title').text($.i18n.prop('partner_Use_the_Coupon'));
	}
	var _id = $.getUrlParam("id");
	listPage.tabHeight=window.innerHeight-90;
	listPage.tab.forEach(function(item){
		if(item.id=='tab1') {
			item.title = $.i18n.prop("Use_the_Coupon_In_Use");
		}else if(item.id=='tab2') {
			item.title = $.i18n.prop("Use_the_Coupon_Not_Used");
		}else if(item.id=='tab3') {
			item.title = $.i18n.prop("Use_the_Coupon_Written_Off");
		}else if(item.id=='tab4') {
			item.title = $.i18n.prop("Use_the_Coupon_Expired");
		}
		if(_id) {
			listPage[item.id].search['user.customer.id']=_id;
		}
		mui('#pullrefresh_'+item.id).pullRefresh({
			down:{
				style:'circle',
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback:pulldownRefresh
			},
			up:{
				auto:true,
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback: pullupRefresh
			}
		});
	});
	// mui('.mui-scroll-wrapper').scroll();
	mui('.mui-slider').on('tap','li',function(){
		var _id=$(this).attr('data-id');
		mui.openWindow({
			url: '../coupon/info.html?id=' + _id
		});
	});
	mui('.mui-popover').on('tap', '#menuScan', function(){
		mui.openWindow({
			url: "dahua://couponScan?dtId="+listPage.uid
		});
	});
	mui('.mui-popover').on('tap', '#menuInput', function(){
		var  btnArray = [$.i18n.prop('close'),  $.i18n.prop('ercizhuc_Yes')];
		mui.prompt($.i18n.prop('User_Coupon_lbl15'),  $.i18n.prop('User_Coupon_lbl16'),  
			$.i18n.prop('User_Center_Manual_Entry'), btnArray,  function(e) {
		    if(e.index == 1) {
		    	if(e.value=='') {
		    		mui.toast($.i18n.prop('User_Coupon_lbl16'));
		    	}else {
		    		showCouponInfo(e.value);
		    	}
		    	return false;
		    }
		});
	});
	function showCouponInfo(code, obj) {
		mui.ajax('/customer/coupon/get.json', {
			data: {
				'code': code,
				'dt.id': listPage.uid
			},
			dataType: 'json',
			type:'post',
			success: function(data) {
				if('1'==data.status){
					if(null==data.object) {
						mui.toast($.i18n.prop('User_Coupon_lbl17'));
						return;
					}
					mui.openWindow({
						url: '../coupon/info.html?code=' + code
					});
				}
			}
		});
	}
	// mui.init({
	// 	pullRefresh:{
	// 		container:'#pullrefresh_tab1',
	// 		down:{
	// 			style:'circle',
	// 			contentdown: $.i18n.prop('contentDown'),
	// 			contentover: $.i18n.prop('contentOver'),
	// 			contentrefresh: $.i18n.prop('contentRefresh'),
	// 			contentnomore: $.i18n.prop('noData'),
	// 			callback:pulldownRefresh
	// 		},
	// 		up:{
	// 			auto:true,
	// 			contentdown: $.i18n.prop('contentDown'),
	// 			contentover: $.i18n.prop('contentOver'),
	// 			contentrefresh: $.i18n.prop('contentRefresh'),
	// 			contentnomore: $.i18n.prop('noData'),
	// 			callback: pullupRefresh
	// 		}
	// 	}
	// });
	// 上拉加载
	function pulldownRefresh(){
		// mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);//参数为true代表没有更多数据了.
		var self = this, _tabItem=$(this.element).attr('data-val');
		listPage[_tabItem].receiptedPageNo = 1;
		listPage[_tabItem].receipted = [];
		listPage[_tabItem].noReceiptedData = "";
		var _param=listPage[_tabItem].search;
		_param['dt.id']=listPage.uid;
		_param.pageNo='1';
		
		mui.ajax('/customer/coupon/list.json', {
			data: _param,
			dataType: 'json',
			type:'post',
			success: function(data) {
				var records = data.records;
				listPage[_tabItem].receiptedTotal = data.totalCount;
				if(null == records || records.length == 0) {
					listPage[_tabItem].receipted = [];
					listPage[_tabItem].noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage[_tabItem].receipted = records;
					listPage[_tabItem].noReceiptedData = "";
					listPage[_tabItem].receiptedPageNo = 2;
				}
				i18nReady();
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPulldownToRefresh(false);
			}
		});
	}
	// 下拉刷新
	function pullupRefresh(){
		// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		// mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);//参数为true代表没有更多数据了.
		var self = this, _tabItem=$(this.element).attr('data-val');
		listPage[_tabItem].noReceiptedData = "";
		var _param=listPage[_tabItem].search;
		_param['dt.id']=listPage.uid;
		_param.pageNo=listPage[_tabItem].receiptedPageNo;
		
		mui.ajax('/customer/coupon/list.json', {
			data: _param,
			dataType: 'json',
			type:'post',
			success: function(data) {
				console.log(data);
				var records = data.records;
				listPage[_tabItem].receiptedTotal = data.totalCount;
				if((listPage[_tabItem].receiptedPageNo <= data.totalPage) && (null != records)) {
					listPage[_tabItem].receipted = listPage[_tabItem].receipted.concat(records);
					listPage[_tabItem].receiptedPageNo++;
				} else {
					mui.toast($.i18n.prop('noData'));
				}
				if(data.totalCount == 0) {
					listPage[_tabItem].noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage[_tabItem].noReceiptedData = "";
				}
				i18nReady();
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPullupToRefresh(false);
			}
		});
	}
});
